Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Themes] Refactor CLI server events for Hot Reload (PoC Hot Reload for OSE) #4908

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented Nov 22, 2024

Goals: support hot reload for Online Store Editor and Theme Preview.

Full context here.

The changes in this repo are:

  • Move the hot reload decisions from the server to the client. For example, instead of sending an event for "CSS update" from the server to the client, now the server sill simply say "here, this file has been changed". The client will check the extension of the file and decide to perform a CSS update, or something else.
  • The server now emits 2 events per file: once when the file is updated locally, and another one when it's uploaded to the cloud. This means that the client can choose to refresh earlier or later, depending of what it needs. For example, when running on localhost, hot reload will happen as soon as a file is updated locally because we can access the content. However, in OSE, we will wait for the "cloud sync" event before triggering a refresh.
  • Implement handling for events in the client for multiple situations: CLI, OSE, Theme Preview. This is done in https://github.com/shopify/theme-hot-reload/
  • If the hot reload frontend is not injected by SFR, the CLI will inject it. This way we can release without depending on SFR deploys.

🎩 :

  • shopify theme dev and check that it downloads the theme-hot-reload script in the network tab.
  • Hot reload should keep working the same in localhost for every file type.

Copy link
Contributor

github-actions bot commented Nov 22, 2024

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
75.32% (-0.05% 🔻)
8889/11801
🟡 Branches
70.56% (-0.04% 🔻)
4322/6125
🟡 Functions
75.1% (-0.05% 🔻)
2337/3112
🟡 Lines
75.86% (-0.04% 🔻)
8401/11074
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / app-event-watcher.ts
93.83% (-1.23% 🔻)
86.49% (-2.7% 🔻)
90.48% 98.61%
🟡
... / build.ts
78.51% (-0.18% 🔻)
64.58% 78.38%
76.99% (-0.2% 🔻)
🟢
... / ConcurrentOutput.tsx
98.36% (-1.64% 🔻)
88% (-4% 🔻)
100%
98.33% (-1.67% 🔻)
🟡
... / fs.ts
66.67% (-2.22% 🔻)
93.75%
63.64% (-2.27% 🔻)
65.91% (-2.27% 🔻)
🟢
... / git.ts
88.73% (-1.51% 🔻)
87.18% (-1.71% 🔻)
87.5% (-1.39% 🔻)
90% (-1.25% 🔻)

Test suite run success

2001 tests passing in 904 suites.

Report generated by 🧪jest coverage report action from 8657282

@frandiox
Copy link
Contributor Author

/snapit

Copy link
Contributor

🫰✨ Thanks @frandiox! Your snapshot has been published to npm.

Test the snapshot by intalling your package globally:

pnpm i -g @shopify/[email protected]

After installing, validate the version by running just shopify in your terminal
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

@frandiox frandiox marked this pull request as ready for review January 16, 2025 19:42
@frandiox frandiox requested a review from a team as a code owner January 16, 2025 19:42
Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, @frandiox 🔥 Amazing work as usual!

I couldn't spot any issues with themes neither theme app extension. Given the nature of this change tho, I'd love to expand these tests even further.

When we feel ready, I believe we can snapit this branch and share a build with our partners to gather their feedback before the release. I'm sure they will love having hot reload on OSE, so we will likely have a wide audience.

Regarding these changes:

  • Move the hot reload decisions from the server to the client. For example, instead of sending an event for "CSS update" from the server to the client, now the server sill simply say "here, this file has been changed". The client will check the extension of the file and decide to perform a CSS update, or something else.

This is very neat. The more I look at the client.ts file, the more it feels like it should be a standalone library. I can see it being a dependency for the CLI and for the OSE/theme-preview, and I can also see third-party tooling being built on top of it.

Having that as a standalone library might also give us more room to separate the logic and version those APIs—keeping in mind that OSE won't have guarantees about the CLI version.

  • The server now emits 2 events per file: once when the file is updated locally, and another one when it's uploaded to the cloud

This is an excellent decision! ✨

Please, let me know your thoughts about this! Thanks again for this PR!

@frandiox frandiox requested a review from a team as a code owner January 21, 2025 14:34
Copy link
Contributor

We detected some changes at packages/*/src and there are no updates in the .changeset.
If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.

@frandiox frandiox changed the title [Themes] PoC Hot Reload for OSE [Themes] Refactor CLI server events for Hot Reload (PoC Hot Reload for OSE) Jan 23, 2025
Copy link
Contributor

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

We found no new type declarations in this PR

Existing type declarations

packages/cli-kit/dist/public/node/themes/types.d.ts
@@ -4,18 +4,19 @@ import { AdminSession } from '../session.js';
  */
 export type Key = string;
 export type ThemeFSEventName = 'add' | 'change' | 'unlink';
+type OnSync = (onSuccess: () => void, onError?: () => void) => void;
 type ThemeFSEvent = {
     type: 'unlink';
     payload: {
         fileKey: Key;
-        onSync?: (fn: () => void) => void;
+        onSync: OnSync;
     };
 } | {
     type: 'add' | 'change';
     payload: {
         fileKey: Key;
         onContent: (fn: (content: string) => void) => void;
-        onSync: (fn: () => void) => void;
+        onSync: OnSync;
     };
 };
 export type ThemeFSEventPayload<T extends ThemeFSEventName = 'add'> = (ThemeFSEvent & {

@frandiox frandiox requested a review from karreiro January 24, 2025 19:09
Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this PR, @frandiox! 🚀 It looks great! I believe we just need to handle those minor issues on the editor side, and then we can proceed with the merge here :)

Also, I've left a minor question about versioning events 🙇

@@ -50,6 +50,7 @@
"yaml": "2.7.0"
},
"devDependencies": {
"@shopify/theme-hot-reload": "^0.0.5",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should version the events here somehow? (in runtime)

This way, if we need to change something, we can prevent older versions of the CLI from breaking (we will likely need to keep triggering old events if we really change the shape of them)

@benjaminsehl
Copy link
Member

/snapit

Copy link
Contributor

🫰✨ Thanks @benjaminsehl! Your snapshot has been published to npm.

Test the snapshot by intalling your package globally:

pnpm i -g @shopify/[email protected]

After installing, validate the version by running just shopify in your terminal
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants